﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mersiv.Lib.Wrapper;

namespace Mersiv.App.Client.Controls
{
  public partial class AddRepositoryForm : Form
  {

    public HgRepository Repository { get; set; }


    public AddRepositoryForm()
    {
      InitializeComponent();
      this.Repository = new HgRepository();
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
      this.DialogResult = DialogResult.OK;
    }

    private void btnBrowse_Click(object sender, EventArgs e)
    {
      FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
      folderBrowserDialog.Description = "Add Existing Local Repository";
      folderBrowserDialog.SelectedPath = Properties.Settings.Default.PreviousRepositoryPath;
      if (DialogResult.OK == folderBrowserDialog.ShowDialog(this))
      {
        this.Repository.WorkingDirectory = folderBrowserDialog.SelectedPath;
        this.txtPath.Text = this.Repository.WorkingDirectory;
        if (string.IsNullOrEmpty(this.txtName.Text))
        {
          // GetFileName returns the folder name when given a directory path
          this.txtName.Text = Path.GetFileName(this.Repository.WorkingDirectory);
        }
      }
    }

    private void txtName_TextChanged(object sender, EventArgs e)
    {
      this.Repository.Name = this.txtName.Text;
      this.Enable();
    }

    private void txtPath_TextChanged(object sender, EventArgs e)
    {
      this.Repository.WorkingDirectory = this.txtPath.Text;
      this.Enable();
    }

    private void Enable()
    {
      if (string.IsNullOrEmpty(this.txtPath.Text) || string.IsNullOrEmpty(this.txtName.Text))
      {
        this.btnOK.Enabled = false;
      }
      else
      {
        this.btnOK.Enabled = true;
      }
    }

  }
}